/************************************************************
** Tim Huerter
** CS 235
** Final Project
** Description:   Test driver used to develop main functionality
**
************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include "./studentList.h"
#include <ctype.h>
#include <signal.h>

// Comment out the line below to use the real menu
//#define TEST_DRIVER

#define MAXOPT 80
int optNum = 1;
const char* options[MAXOPT] = {
   [0] = "quit",
   [1] = "display options",
   [2] = "print gradebooks",
   [3] = "print students",
   [4] = "add student",
   [5] = "find student",
   [6] = "delete student",
   [7] = "edit student",
   [8] = "get next student",
   [9] = "get previous student",
   [10]= "read gradebook file",
};

const int totalOpts = 11;

gradebookType currentBook;

void printOptions(void) {
   int i = 0;
   printf("Manage Students:\n");
   for(i=0; i<totalOpts; i++) {
      printf("   '%d' : %s\n", i, options[i]);
   }
}

#ifndef TEST_DRIVER


/************************************************************
** function:      createGradeBook
** description:	This function will create a grade book name
**						and prompt the user to add the grading structure
**						and students if the user desires.
************************************************************/
void createGradeBook(){
	char yesNo[2];
	bool valid = true;
   studentType temp = {
      "Master",
      "Master",
      -1,
      NULL,
      NULL,
      NULL,
   };
	gradeType tempGrade;
	
	strcpy(fileName,"");  // clear the filename
	printf("Enter the gradebook name: ");
	scanf(" %[^\n]s",currentBook.name);
	strcpy(fileName,currentBook.name);  //copy so that we can create the file later
	fflush(stdin);

   currentBook.master = initStudentList(temp);
   // Prompt user to add the grades for the structure
	printf("Enter the grade fields for the gradebook\n");
	printf("Enter the first grade field name: ");
	scanf(" %[^\n]s",tempGrade.name);
	printf("Enter the total possible score: ");
	scanf(" %f", &tempGrade.grade);

   addGrade(tempGrade, currentBook.master->studentGrades);
	printf("Field successfully added!\n");
	while (valid) {
		strcpy(yesNo,"y");
		printf("\nAdd another grade field? (Y/N):\n");
		scanf("%s",yesNo);
		// if no then stop the loop and don't add any grades
		if (tolower(yesNo[0]) == 'n') {
			while (valid){
				 memset(&tempGrade, 0, sizeof(tempGrade));
				 printf("\nAdd a student? (Y/N):\n");
				 canf("%s",yesNo);
				 if (tolower(yesNo[0]) == 'n') {
				 	valid = false;
				 }
				 else if (tolower(yesNo[0]) != 'y') {
					 invalidEntry();
				 }
				 else {
					 printf("Enter the students first name: ");
					 scanf(" %[^\n]s",temp.firstname);
					 printf("Enter the students last name: ");
					 scanf(" %[^\n]s",temp.lastname);
					 addStudent(temp);
				}	
			}
		}
		else if (tolower(yesNo[0]) != 'y') {
			invalidEntry();
		}
		// if yes then continue the loop to add grades
		else {
         memset(&tempGrade, 0, sizeof(tempGrade));
			printf("Enter the grade field name: ");
			scanf(" %[^\n]s",tempGrade.name);
         printf("Enter the total possible score: ");
         scanf(" %f", &tempGrade.grade);
         addGrade(tempGrade, currentBook.master->studentGrades);
			printf("Field successfully added!\n");
		}
	}
   // call next menu
   studentMenu();
}
#endif

void quit() {
   deallocateStudents();
   printf("GOODBYE!\n");
   exit(SIGTERM);
}

#ifndef TEST_DRIVER
int main(int argc, char *argv[])
{
   int selection;
   int id = 0;
   studentType temp, *temp2;
   char names[MAXNAME];
   bool bookOpen = false;
   bool valid = false;
	char yesNo[2];

	while (1) {
		printf("\n*****************************************\n");
   	printf("*********    Gradebook CS235    *********\n");
   	printf("*****************************************\n");
   	printf("Main Menu:\n");
   	printf("    1. - Create a new gradebook\n");
   	printf("    2. - Open a gradebook\n");
   	printf("    3. - quit\n");
   
   	valid = false;
   
   	while (valid == false) {
   		printf("Select an option: ");
   		scanf("%d",&selection);
   		getchar();  // clear the \n from the buffer
   		switch (selection) {
   			case 1:
   				valid = true;
   				createGradeBook();
   				break;
   			case 2:
   				valid = true;
   	   	   loadGradeBooks();
   				break;
   			case 3:
               quit();
   				valid = true;
   				break;
   			default:
   				invalidEntry();
		   }		
   	}
   }
   fflush(stdin);
   deallocateStudents();
}
#endif

#ifdef TEST_DRIVER
int main(int argc, char *argv[])
{
   char selection[10];
   int id = 0;
   studentType temp, *temp2, *master;
   gradeType tempGrade;
   char names[MAXNAME];
   bool bookOpen = false;

   strcpy(temp.firstname, "Master");
   strcpy(temp.lastname, "Master");
   temp.studentGrades = NULL;
   temp.prev = NULL;
   temp.next = NULL;

   (void)initStudentList(temp); // initialize the studentList.
	master = getMaster();
	
	strcpy(tempGrade.name, "Homework 1");
	tempGrade.grade = 10.0;
	
	addGrade(tempGrade, master->studentGrades);
   
	strcpy(tempGrade.name, "Homework 2");
	tempGrade.grade = 20.0;
	addGrade(tempGrade, master->studentGrades);

   printStudent(master);
   printGrades(master->studentGrades);
   printf("master initilized\n\n");

   while (1) {
   	//verify that only one letter is entered
   	// if (strlen(selection)==1)
      // switch (selection[0]) {
      switch (optNum) {
         case 0: // display options
            quit();
            return;
         case 1: // display options
            // mainMenu_1();
            printOptions();
            break;
         case 2: //print gradebooks
            printf("\nSelect a Gradebook:\n");
            // loadGradeBooks();
            bookOpen = true;
            // flag is set so we know what needs to be done if changes don't get completed.
            // go to edit menu
            //editMenu_2();
            break;
         case 3: // print students
            printf("Printing Students\n");
            printStudents(getFirstStudent());
            break;
         case 4: // add student
            printf("Adding Student\n");
            printf("Enter Student's first name: ");
            scanf("%s", temp.firstname);
            // temp.firstname = (char*)malloc(strlen(names));
            // strcpy(temp->firstname, names);
            printf("Enter Student's last name: ");
            scanf("%s", temp.lastname);
            // temp->lastname = (char*)malloc(strlen(names));
            // strcpy(temp->lastname, names);
            addStudent(temp);
            break;
         case 5: // find student
            printf("Finding Student\n");
            printf("Enter the student's ID: ");
            scanf("%d", &id);
            temp2 = getStudent(id);
            if(temp2) {
               printStudent(temp2);
            } else {
               printf("Student %d not found\n", id);
            }
            break;
         case 6: // delete student
            printf("Deleting Student\n");
            printf("Enter the student's ID: ");
            scanf("%d", &id);
            delStudent(id);
            break;
         case 7: // edit student
            printf("Editing Student\n");
            printf("Enter the student's ID: ");
            scanf("%d", &id);
            temp2 = getStudent(id);
            if(temp2) {
               temp2 = editStudent(id);
            } else {
               printf("Student %d not found\n", id);
            }
            break;
         case 8: // get next student
            printf("Retrieving Next Student\n");
            printf("Enter the student's ID: ");
            scanf("%d", &id);
            temp2 = getStudent(id);
            if(temp2) {
               printStudent(temp2);
            } else {
               printf("Student %d not found\n", id);
            }
            printf("next student: \n");
            temp2 = getNextStudent(temp2);
            if(temp2) {
               printStudent(temp2);
            } else {
               printf("Next Student not found\n");
            }
            break;
         case 9: // get previous student
            printf("Retrieving Previous Student\n");
            printf("Enter the student's ID: ");
            scanf("%d", &id);
            temp2 = getStudent(id);
            if(temp2) {
               printStudent(temp2);
            } else {
               printf("Student %d not found\n", id);
            }
            printf("previous student: \n");
            temp2 = getPrevStudent(temp2);
            if(temp2) {
               printStudent(temp2);
            } else {
               printf("Previous Student not found\n");
            }
            break;
		 case 10: // read grade book file
		    //parseCSV("testfile.csv");
            break;
         default:
            printf("Invalid entry!\n");
            break;
      }
      fflush(stdin);
      optNum = 0;
      printf("Enter Option: ");
      scanf("%d", &optNum);
   }
}
#endif

